﻿using StatlightTfsBuildIntegration.Statlight;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using StatlightTfsBuildIntegration.TestResultService;
using System.ServiceModel;
using System.Security.Principal;
using StatlightTfsBuildIntegration.IdentityManagementService;

namespace UnitTests
{


    /// <summary>
    ///This is a test class for TestTest and is intended
    ///to contain all TestTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TestTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private static Guid ReadCurrentUsersIdentity()
        {
            Console.WriteLine("Reading users identity...");
            string url = "http://nlses010:8080/tfs2010/Services/v3.0/IdentityManagementService.asmx";
            using (var client = new IdentityManagementWebServiceSoapClient(
                    new BasicHttpBinding()
                    {
                        Security = new BasicHttpSecurity
                        {
                            Mode = BasicHttpSecurityMode.TransportCredentialOnly,
                            Message = new BasicHttpMessageSecurity
                            {
                                AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default,
                                ClientCredentialType = BasicHttpMessageCredentialType.UserName
                            },
                            Transport = new HttpTransportSecurity()
                            {
                                ClientCredentialType = HttpClientCredentialType.Ntlm,
                                ProxyCredentialType = HttpProxyCredentialType.None,
                            }
                        }
                    },
                        new EndpointAddress(url)
                        ))
            {
                var currentUser = WindowsIdentity.GetCurrent();

                if (currentUser == null)
                {
                    throw new InvalidOperationException("Could not find current Windows user.");
                }

                var result = client.ReadIdentities(0, new[] { currentUser.Name }, 0, 0);

                if ((result == null) || (result.Length != 1) || (result[0].Length != 1))
                {
                    throw new InvalidOperationException(string.Format("Could not find user {0} in TFS.", currentUser.Name));
                }

                return result[0][0].TeamFoundationId;
            }
        }



        /// <summary>
        ///A test for Test Constructor
        ///</summary>
        [TestMethod()]
        public void TestConstructorTest()
        {
            Guid user = ReadCurrentUsersIdentity();
            string urlTemplate = "http://nlses010:8080/tfs2010/TestManagement/v1.0/TestResults.asmx";
            var serviceClient = new TestResultsServiceSoapClient
                    (
                    new BasicHttpBinding()
                    {
                        Security = new BasicHttpSecurity
                        {
                            Mode = BasicHttpSecurityMode.TransportCredentialOnly,
                            Message = new BasicHttpMessageSecurity
                            {
                                AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default,
                                ClientCredentialType = BasicHttpMessageCredentialType.UserName
                            },
                            Transport = new HttpTransportSecurity()
                            {
                                ClientCredentialType = HttpClientCredentialType.Ntlm,
                                ProxyCredentialType = HttpProxyCredentialType.None,
                            }
                        }
                    },
                        new EndpointAddress(urlTemplate)
                        );
            serviceClient.ClientCredentials.UserName.UserName = "vxproject\\dsmits";
            serviceClient.ClientCredentials.UserName.Password = "Welkom01";
            serviceClient.CreateTestRun(new StatlightTfsBuildIntegration.TestResultService.TestRun(), "DefaultCollection");
        }

		[TestMethod()]
		public void TestResult()
		{
			var testRun = TestBuilder.Parse("D:\\VX\\TFS\\VXDOTNET\\Dave\\TFSBuildActivities\\StatlightTfsBuildIntegration\\Statlight\\TestResult.xml");
			foreach (var t in testRun.TestList)
			{
				var result = t.Result;
			}
		}


    }
}
